package com.test.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.test.util.TokenProccessor;

/**
 * 拦截器
 * @ClassName: LoginInterceptor 
 * @Description: TODO 
 * @author weiqi 
 * @date 2017年3月19日 下午6:51:21
 */
public class LoginInterceptor implements HandlerInterceptor{
	
	// 日志
	protected Logger log = Logger.getLogger(getClass());

	public final static String SESSION_USER = "session_user";

	public final static String SESSION_COMPANY = "session_company";
	public final static String SESSION_MENU = "session_menu";
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		HttpSession session = request.getSession();
		String url = request.getServletPath().toString();
		//是登录就放行
		if(url.equals("/user/validateLogin")){
			return true;
		}
		//session过期也会被拦截
		if (null == session.getAttribute(SESSION_USER) || null == session.getAttribute(SESSION_MENU)) {
			
			//重定向到登录界面
			response.sendRedirect(request.getContextPath()+"/user/user_login");
		}
		return true;
	}
	
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {		
	}
}
